<?php
require_once $_SESSION["app_root"]."config/app.config.php";
class UserDAO
{
	public function Authenticate($Email, $Password){
		$user = null;
		$db = GetDatabase();
		$query = "	SELECT 
						cms_user_accounts.*,
						cms_user_groups.GroupID AS GroupID
					FROM 
						cms_user_accounts  LEFT JOIN 
						cms_user_groups ON cms_user_accounts.ID = cms_user_groups.UserID AND IsDefault = 1
					WHERE Email = '".$Email."' AND Password = '".md5($Password)."'";
		$rs = $db->SelectLimit($query, 1, 0);
		$result = $rs->FetchRow();
		if($result){
			$user = $this->UserReader($result);
		}
		return $user;
	}
	
	public function GetByID($ID){
		$user = null;
		$db = GetDatabase();
		$query = "	SELECT 
						cms_user_accounts.*,
						cms_user_groups.GroupID AS GroupID
					FROM 
						cms_user_accounts LEFT JOIN 
						cms_user_groups ON cms_user_accounts.ID = cms_user_groups.UserID AND IsDefault = 1
					WHERE ID = '".$ID."'";
		$rs = $db->SelectLimit($query, 1, 0);
		$result = $rs->FetchRow();
		if($result){
			$user = $this->UserReader($result);
		}
		return $user;
	}
	
	public function UserReader($result){
		$user = new UserDVO($result['ID'], 
							$result['Email'], 
							null,
							$result['FirstName'], 
							$result['LastName'], 
							$result['Role'],
							$result['GroupID']);
		return $user;
	}
}
?>